package com.qztc.news_end_customer.controller;

import com.qztc.news_end_customer.config.GlobalConfig;

import com.qztc.news_end_dto.dto.NewsCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/thenews/newscollection")
public class NewsCollectionControllerByRestTemplate extends GlobalConfig {
     @Autowired
     private RestTemplate restTemplate;

     @PostMapping("/add")
     public String addNewsCollection(@RequestBody NewsCollection newsCollection) {
         HttpHeaders headers = new HttpHeaders();
         headers.setContentType(MediaType.APPLICATION_JSON);
         HttpEntity<NewsCollection> httpEntity = new HttpEntity<>(newsCollection,headers);
         ResponseEntity<String> result = restTemplate.exchange(SEVICE_URL+"newscollection/add", HttpMethod.POST,httpEntity,String.class);
         return result.getBody();
     }

     @PostMapping("/getbyuserid")
     public String getNewsCollectionByUserId(@RequestBody NewsCollection newsCollection) {
         HttpHeaders headers = new HttpHeaders();
         headers.setContentType(MediaType.APPLICATION_JSON);
         HttpEntity<NewsCollection> httpEntity = new HttpEntity<>(newsCollection,headers);
         ResponseEntity<String> result = restTemplate.exchange(SEVICE_URL+"newscollection/getbyuserid", HttpMethod.POST,httpEntity,String.class);
         return result.getBody();
     }

     @DeleteMapping("/deletebyuserid_newsid")
     public String delete(@RequestBody NewsCollection newsCollection) {
         HttpHeaders headers = new HttpHeaders();
         headers.setContentType(MediaType.APPLICATION_JSON);
         HttpEntity<NewsCollection> httpEntity = new HttpEntity<>(newsCollection,headers);
         ResponseEntity<String> result = restTemplate.exchange(SEVICE_URL+"newscollection/deletebyuserid_newsid", HttpMethod.DELETE,httpEntity,String.class);
         return result.getBody();
     }

     @GetMapping("/get")
     public String getNewsCollection() {
         return restTemplate.getForObject(SEVICE_URL+"newscollection/get", String.class);
     }
}
